<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\V3\V3_1\Models\MaterielModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CommonDetailController extends Controller
{
    /**
     * @采购单详细 without token
     * @created 在 2021/12/9 13:52 创建
     * @param Request $request
     * @author: Sun Wu
     */
    public function materielDetail(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->input(), [
            'id' =>'required|integer',//采购单id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $info = MaterielModel::select(
            'a.*',
            'b.nickname',
            'b.username',
        )
            ->with('materielContent', function($query) {
                $query->select(
                    'materiel_id',
                    'unit_price',
                    'num',
                    'a.materiel_name',
                    'a.type_name',
                    'a.materiel_no',
                    'a.materiel_img',
                    //'rest_num',
                    'b.storage_name',
                )
                ->leftJoin('materiel_type as a', 'a.id', '=', 'materiel_content.type_id')
                ->leftJoin('storage as b', 'b.id', '=', 'materiel_content.storage_id');
            })
            ->from('materiel as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->where('a.id', $data['id'])
            ->where('a.delete_flag', 0)
            ->orderBy('a.id', 'desc')
            ->get()->toArray();

        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => $info
        ]);
    }

    public function materielDetailIndex()
    {
        return view('purchase');
    }

    //采购单详情-html
    public function customerSend()
    {
        return view('customerSend');
    }

    public function sales()
    {
        return view('sales');
    }

    public function salesReturn()
    {
        return view('salesReturn');
    }

    public function templateCustomerSend()
    {
        return view('templateCustomerSend');
    }

    public function dispatchBill()
    {
        return view('dispatchBill');
    }

    public function wxPage()
    {
        return view('wxPage');
    }

    public function verification()
    {
        return view('verification');
    }

    public function wxSetting(Request $request)
    {
        ob_clean();
        $signature = $request->input("signature");
        $timestamp = $request->input("timestamp");
        $nonce = $request->input("nonce");

        $token = config('app.weixin_token');

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return $request->input("echostr");
        } else {
            return false;
        }
    }
}
